from sympy.core import Number
from sympy.geometry import Plane, Point, Point3D, Point2D, Polygon
from sympy import acos, trigsimp
from sympy.matrices import Matrix


def angle(p1, p2, vertise=None) -> Number:
    if len(p1) == len(p2):
        dim = len(p1)
    else:
        raise ValueError("The dimenssion of two points is wrong.")
    if vertise == None:
        p0 = Point([0] * dim)

    cross = Matrix(p1).dot(Matrix(p2)) / (p1.distance(p0) * p2.distance(p0))
    return acos(trigsimp(cross))


def points_to_3d(points: list[Point]) -> list[Point3D]:
    if isinstance(points, Point):
        points = [points]

    return [Point(a, dim=3) for a in points]


def points_to_2d(points: list[Point]) -> list[Point2D]:
    bottom_point3d = []
    if isinstance(points, Point):
        points = [points]
    for point in points:
        if len(point) > 2:
            point = Point(*point[0:2])
        bottom_point3d.append(point)
    return bottom_point3d


def polygon_to_plane(polygon: Polygon) -> Plane:
    return Plane(*polygon.vertices[0:3])
